#include "ohos_video_source.h"
#include "ohos_video_source_adapter.h"
#include "pc/video_track_source_proxy.h"
#include "api/media_stream_interface.h"
#include "rtc_base/checks.h"

namespace ohoswebrtc {

OHOSVideoSource::OHOSVideoSource(
    rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source, rtc::scoped_refptr<OHOSPeerConnectionFactoryInterface> peer_connection_factory)
    : video_source_(video_source),
      peer_connection_factory_(peer_connection_factory) {
  RTC_LOG(LS_INFO) << __FUNCTION__ << ": Ctor";
}

OHOSVideoSource::OHOSVideoSource(rtc::scoped_refptr<OHOSPeerConnectionFactoryInterface> peer_connection_factory, rtc::Thread* signaling_thread, rtc::Thread* worker_thread, const bool is_screen_cast) 
    : peer_connection_factory_(peer_connection_factory) {
  rtc::scoped_refptr<OHOSVideoTrackSourceAdapter> ohos_video_track_source =
      rtc::make_ref_counted<OHOSVideoTrackSourceAdapter>(is_screen_cast);
  video_source_ = webrtc::VideoTrackSourceProxy::Create(signaling_thread,  worker_thread, ohos_video_track_source);
}

void OHOSVideoSource::OnCapturedFrame(const webrtc::VideoFrame &frame) {
  webrtc::VideoTrackSourceProxy *proxy_source =
      static_cast<webrtc::VideoTrackSourceProxy *>(video_source_.get());
  OHOSVideoTrackSourceAdapter *video_track_source_adapter = static_cast<OHOSVideoTrackSourceAdapter *>(proxy_source->internal());
  video_track_source_adapter->OnFrameCaptured(frame);
}

bool OHOSVideoSource::is_screencast() {
  if (video_source_) {
    return video_source_->is_screencast();
  }
  return false;
}

OHOSVideoSource::~OHOSVideoSource() {
  RTC_LOG(LS_INFO) << __FUNCTION__ << ": Dtor";
}

}  // namespace ohoswebrtc
